<template>
  <div style="width: 700px; margin: 0px auto">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      size="small"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
      label-position="right"
    >
      <el-form-item label="用户名">
        <el-input readonly v-model="username" placeholder="用户名"></el-input>
        <span class="civ-form-extra">后台登录用户名</span>
      </el-form-item>

      <el-form-item label="姓名">
        <el-input readonly v-model="name" placeholder="姓名"></el-input>
        <span class="civ-form-extra">管理员姓名</span>
      </el-form-item>

      <el-form-item label="原密码" prop="password">
        <el-input
          type="password"
          v-model="dataForm.password"
          placeholder="原密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          type="password"
          v-model="dataForm.newPassword"
          placeholder="新密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          type="password"
          v-model="dataForm.confirmPassword"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="dataFormSubmit()">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { clearLoginInfo } from "@/utils";
import { updateLoginUserPassword } from "@/api/login_user";
export default {
  data() {
    var validateConfirmPassword = (rule, value, callback) => {
      if (this.dataForm.newPassword !== value) {
        callback(new Error("确认密码与新密码不一致"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      dataForm: {
        password: "",
        newPassword: "",
        confirmPassword: "",
      },
      dataRule: {
        password: [
          { required: true, message: "原密码不能为空", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { validator: validateConfirmPassword, trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.init();
  },
  computed: {
    name: {
      get() {
        return this.$store.state.user.name;
      },
    },
    username: {
      get() {
        return this.$store.state.user.username;
      },
    },
    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs;
      },
      set(val) {
        this.$store.commit("common/updateMainTabs", val);
      },
    },
  },
  methods: {
    // 初始化
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          updateLoginUserPassword({
            oldPassword: this.dataForm.password,
            newPassword: this.dataForm.newPassword,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$nextTick(() => {
                    this.mainTabs = [];
                    clearLoginInfo();
                    this.$router.replace({ name: "login" });
                  });
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>

